﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TimbreRecognition.Recognition.Model.Kohenen
{
    public class KohenenNetwork
    {
        public int Width { get; set; }

        public int Height { get; set; }

        private KohenenNeuron[][] neurons;

        public KohenenNetwork()
        {
        }

        public KohenenNeuron getWinner() {
            KohenenNeuron winner = null;
            double maxOutput = 0;
            foreach (KohenenNeuron[] currentNeurons in neurons) {
                foreach (KohenenNeuron currentNeuron in currentNeurons) {
                    double output = currentNeuron.getOutput();
                    if (output > maxOutput) {
                        maxOutput = output;
                        winner = currentNeuron;
                    }
                }
            }

            return winner;
        }

        public void recalculateOutput(double[] input) {
            foreach (KohenenNeuron[] currentNeurons in neurons) {
                foreach (KohenenNeuron currentNeuron in currentNeurons) {
                    currentNeuron.recalculateOutput(input);
                }
            }
        }

        public KohenenNeuron[][] getNeurons()
        {
            return neurons;
        }

        public void setNeurons(KohenenNeuron[][] neurons)
        {
            this.neurons = neurons;

            this.Width = neurons.Length;
            this.Height = neurons.GetLength(0);
        }

        public int getYDimension()
        {
            return neurons.Length;
        }

        public int getXDimension()
        {
            return neurons[0].Length;
        }

        public void resetWinCount()
        {
            foreach (KohenenNeuron[] currentNeurons in neurons) {
                foreach (KohenenNeuron currentNeuron in currentNeurons) {
                    currentNeuron.setWinCount(0);
                }
            }
        }

        public void ResetWinCountForClasses()
        {
            foreach (KohenenNeuron[] currentNeurons in neurons)
            {
                foreach (KohenenNeuron currentNeuron in currentNeurons)
                {
                    currentNeuron.ResetWinCountForClasses();
                }
            }
        }
    }
}
